<template>
    <div v-if="show1">
        <el-row>
            <el-col :span="24">
                <el-row style="text-align: left; margin: 10px 0">
                    <el-col :span="7">ID:<el-input v-model="search.id" style="width: 240px" placeholder="请输入ID" clearable /></el-col>
                    <el-col :span="7">标题:<el-input v-model="search.title" style="width: 240px" placeholder="请输入标题" clearable /></el-col>
                    <el-col :span="2">
                        <el-button type="primary" @click="sosuo()">搜索</el-button>
                    </el-col>
                </el-row>
            </el-col>
            <el-col style="text-align: left; margin: 0 0 10px 0"><el-button type="primary" #default="scope" @click="add(scope)">新增心理测评</el-button></el-col>
            <el-col>
                <el-table :data="arrayList" border style="width: 80%">
                    <el-table-column prop="id" label="ID" align="center" label-class-name="header" />
                    <el-table-column prop="title" label="标题" align="center" label-class-name="header" />
                    <el-table-column prop="img" label="图片" align="center" label-class-name="header">
                        <template #default="scope">
                            <el-image style="width: 100px; height: 100px" :src="scope.row.img" :fit="fit" />
                        </template>
                    </el-table-column>
                    <el-table-column prop="del" label="上下架" align="center" label-class-name="header">
                        <template #default="scope">
                            <div v-if="scope.row.del == 1" class="up">上架</div>
                            <div v-else class="down">下架</div>
                        </template>
                    </el-table-column>
                    <el-table-column label="操作" align="center" label-class-name="header">
                        <template #default="scope">
                            <el-button type="text" @click="edit(scope)">编辑</el-button>
                            <el-button type="text" @click="remove(scope)">删除</el-button>
                            <el-button type="text" v-if="scope.row.del == 0" @click="shangjia(scope)">立即上架</el-button>
                            <el-button type="text" v-else @click="shangjia(scope)">立即下架</el-button>
                            <el-button type="text" @click="change(scope)">查看详情</el-button>
                        </template>
                    </el-table-column>
                </el-table>
                <el-pagination layout="prev, pager, next" :total="10">
                </el-pagination>
            </el-col>
            <el-dialog v-model="show" :title="title" width="600" style="text-align: left">
                <BianJi :key1="ceping.id" v-if="title == '编辑'" :ceping="ceping" @operate="operateAction"></BianJi>
                <ShanChu :key2="ceping.id" v-else-if="title == '提示'" :ceping="ceping" @delate="Delate"></ShanChu>
                <XinZeng v-else @add="Add"></XinZeng>
            </el-dialog>
        </el-row>
    </div>
    <div v-else>
        <CepingItem :ids="ceping" @change2="Change"></CepingItem>
    </div>
</template>

<script setup>
    import axios from "axios";
    import { onMounted, ref, reactive } from "vue";
    import qs from "qs";
    import ShanChu from "./components/ShanChu";
    import XinZeng from "./components/XinZeng.vue";
    import BianJi from "./components/BianJi.vue";
    import CepingItem from "./CepingItem.vue";
    let show = ref(false);
    let show1 = ref(true);
    let arrayList = ref([]);
    let title = ref("");
    let ceping = ref();
    async function start() {
        let res = await axios.get("http://localhost:8080/ceping/select2");
        arrayList.value = res.data.data;
        console.log(arrayList.value);

        // for (let index = 0; index < arrayList.value.length; index++) {
        //     const id = arrayList.value[index].id;
        //     let res1 = await axios.get(`http://192.168.11.196:8080/qingxu/select?id=${id}`)
        //     console.log(res1.data.length);
        //     arrayList.value[index] = res1.data
        //     console.log(arrayList);
        // }
    }
    onMounted(function () {
        start();
    });

    let shangjia = async function (scope) {
        let a = scope.row.del == 1 ? 0 : 1;
        console.log(a);
        console.log(scope.row.del);
        await axios.post(
            "http://localhost:8080/ceping/update",
            qs.stringify({
                ...scope.row,
                del: a,
                id: scope.row.id
            })
        );
        start();
    };
    const Delate = async function (data) {
        console.log(data);

        if (data) {
            await axios.post(
                "http://localhost:8080/ceping/delete",
                qs.stringify({
                    ...data
                })
            );
            let index = arrayList.value.findIndex((item) => item.id == data.id);
            arrayList.value.splice(index, 1);
        }
        show.value = false;
        start();
    };
    const Add = async function (data) {
        if (data) {
            await axios.post(
                "http://localhost:8080/ceping/add",
                qs.stringify({
                    ...data
                })
            );
        }
        show.value = false;
        start();
    };
    const operateAction = async function (data) {
        console.log(data);

        if (data) {
            await axios.post(
                "http://localhost:8080/ceping/update",
                qs.stringify({
                    ...data
                })
            );
        }
        show.value = false;
        start();
    };
    const remove = function (value) {
        title.value = "提示";
        ceping.value = value.row;
        show.value = true;
    };
    const add = function () {
        show.value = true;
        title.value = "新增";
    };
    const edit = function (value) {
        show.value = true;
        title.value = "编辑";
        ceping.value = value.row;
    };
    const change = function (value) {
        show1.value = false;
        ceping.value = value.row.id;
    };
    const Change = function () {
        show1.value = true;
    };
    const search = reactive({
        id: "",
        title: ""
    });

    const sosuo = async function () {
        let res = await axios.get(
            `http://localhost:8080/ceping/selectAll?id=${search.id}&title=${search.title}`
        );
        console.log(res);
        //修改列表数据
        arrayList.value = res.data;
    };
</script>

<style lang="less" scoped>
    .up {
        padding: 5px;
        background-color: #eafaf0;
        color: #13ce66;
        font-size: 12px;
        width: 50px;
        border: 1px solid #d7f5df;
        border-radius: 5px;
        margin: 0 auto;
    }

    .down {
        padding: 5px;
        background-color: #fceded;
        color: #ff4949;
        font-size: 12px;
        width: 50px;
        border: 1px solid #f9dbdb;
        border-radius: 5px;
        margin: 0 auto;
    }
</style>